package com.cnhis.cloudhealth.clinical.hosnurse.hosexpense.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hosexpense.dao.HosExpenseDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
@SuppressWarnings({"unchecked","rawtypes"})
@Service
public class HosExpenseBo {
	@Resource
	private HosExpenseDao expenseDao;
	@Resource
	private GetNewId getNewId;
	 public ModelVo adviceKeepAccounts(Map map,int currPage,int pageSize)throws Exception{
			ModelVo mo = new ModelVo();
			Page page =new Page();
			try {
				page=expenseDao.adviceKeepAccounts(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(page.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(page.getList());
			return mo;
	 }
	 public ModelVo accountOptions()throws Exception{
		 ModelVo mo = new ModelVo();
			List list =null;
			try {
				list=expenseDao.accountOptions();
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
	 }
	 
	 public ModelVo  feeStatisticsType()throws Exception{
		 ModelVo mo = new ModelVo();
			List list =null;
			try {
				list=expenseDao.feeStatisticsType();
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
	 }
	 public ModelVo checkExpense(Map map)throws Exception{
		 ModelVo mo = new ModelVo();
			try {
				if(expenseDao.checkExpense(map)>0){
					mo.setResult(StaticKeys.OH_SUCCESS);
				}else{
					mo.setResult(StaticKeys.OH_FALSE);
					mo.setResultMsg("病人没有费用明细");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			
			return mo;
	 }
	 public ModelVo checkExpenseGuardLine(Map map)throws Exception{
		 ModelVo mo = new ModelVo();
			List list =null;
			try {
				map.put("param406", expenseDao.param406());
				list=expenseDao.checkExpenseGuardLine(map);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
	 }
	 public ModelVo excludedOffice(Map map)throws Exception{
		     ModelVo mo = new ModelVo();
			List list =null;
			try {
				list=expenseDao.excludedOffice(map);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
	 }
	 //获取费用类别
	 private String getABC02(Map map)throws Exception{
		 return expenseDao.getABC02(map);
	 }
	 public ModelVo medicalAdviceFeeDetail(Map map,int currPage,int pageSize)throws Exception{
		  ModelVo mo = new ModelVo();
			Page pg =null;
			try {
				String acf01 = map.get("ACF01").toString();
						if(!"1".equals(acf01)){
							map.put("ABC02", getABC02(map));
						}
				pg=expenseDao.medicalAdviceFeeDetail(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(pg.getList());
			return mo;
	 }
	 public ModelVo feeDetailStatistics(Map map,int currPage,int pageSize)throws Exception{
		 ModelVo mo = new ModelVo();
			Page pg =null;
			try {
				map.put("lVAE44", expenseDao.getlVAE44(map));
				pg=expenseDao.feeDetailStatistics(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(pg.getList());
			mo.setTotal(pg.getTotalNumber());
			mo.setRecords(pg.getTotalNumber());
			return mo;
	 }
	 public ModelVo dayTypeStatistics(Map map,int currPage,int pageSize)throws Exception{
		 ModelVo mo = new ModelVo();
			Page pg =null;
			try {
				
				pg=expenseDao.dayTypeStatistics(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(pg.getList());
			mo.setTotal(pg.getTotalNumber());
			mo.setRecords(pg.getTotalNumber());
			return mo;
	 }
	 public ModelVo dayBillStatistics(Map map,int currPage,int pageSize)throws Exception{
		 ModelVo mo = new ModelVo();
			Page pg =null;
			try {
				
				pg=expenseDao.dayBillStatistics(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(pg.getList());
			mo.setTotal(pg.getTotalNumber());
			mo.setRecords(pg.getTotalNumber());
			return mo;
	 }
	 public ModelVo monthTypeStatistics(Map map,int currPage,int pageSize)throws Exception{
		 ModelVo mo = new ModelVo();
			Page pg =null;
			try {
				
				pg=expenseDao.monthTypeStatistics(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(pg.getList());
			mo.setTotal(pg.getTotalNumber());
			mo.setRecords(pg.getTotalNumber());
			return mo;
	 }
	 public ModelVo typeStatistics(Map map,int currPage,int pageSize)throws Exception{
		 ModelVo mo = new ModelVo();
			Page pg =null;
			try {
				
				pg=expenseDao.typeStatistics(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(pg.getList());
			mo.setTotal(pg.getTotalNumber());
			mo.setRecords(pg.getTotalNumber());
			return mo;
	 }
	 public ModelVo subitemStatistics(Map map,int currPage,int pageSize)throws Exception{
		 ModelVo mo = new ModelVo();
			Page pg =null;
			try {
				
				pg=expenseDao.subitemStatistics(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(pg.getList());
			mo.setTotal(pg.getTotalNumber());
			mo.setRecords(pg.getTotalNumber());
			return mo;
	 }
	 public ModelVo medicalAdviceValuation(Map map)throws Exception{
		            ModelVo mo = new ModelVo();
					List list =null;
					try {
						list=expenseDao.medicalAdviceValuation(map);
						
					} catch (Exception e) {
						e.printStackTrace();
						mo.setResult(StaticKeys.OH_FALSE);
						return mo;
					}
					mo.setList(list);
					mo.setResult(StaticKeys.OH_SUCCESS);
					mo.setRows(list);
					return mo;
	 }
	 public ModelVo othersAdviceValuation(Map map)throws Exception{
		   ModelVo mo = new ModelVo();
			List list =null;
			try {
				map.put("aDate", DateUtils.getDateStr(new Date())+" 23:59:59");
				list=expenseDao.othersAdviceValuation(map);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
	 }
	 public ModelVo herbAdviceValuation(Map map)throws Exception{
		 ModelVo mo = new ModelVo();
			List list =null;
			try {
				list=expenseDao.herbAdviceValuation(map);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
	 }
	 public ModelVo validationAdviceValuation(Map map)throws Exception{
		 ModelVo mo = new ModelVo();
			List<Map<String,Object>> list =null;
			try {
				map.put("aDate", DateUtils.getDateStr(new Date())+" 23:59:59.999");
				map.put("pValue",expenseDao.getValue177());
				list=expenseDao.validationAdviceValuation(map);
				for (Map<String,Object> m2:list) {
					if(m2.containsKey("vaf01")){
						m2.put("vaf01", m2.get("vaf01").toString());
					}
					if(m2.containsKey("vafid")){
						m2.put("vafid", m2.get("vafid").toString());
					}
					if(m2.containsKey("bby01")){
						m2.put("bby01", m2.get("bby01").toString());
					}
					if(m2.containsKey("bby01a")){
						m2.put("bby01a", m2.get("bby01a").toString());
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
	 }
	 public ModelVo tempAdviceValuation(Map map)throws Exception{
		 ModelVo mo = new ModelVo();
			List list =null;
			try {
				if(!"1".equals(map.get("acf01").toString())){
				list=expenseDao.tempAdviceValuation(map);
				}
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
	 }
	 //Clinic_VAJ1_Auto_Insert
	 //自动记账
	 /**
	  * 
	  * @param lDate
	  * @param lFlag --1= 此项目当天记过帐，自动记账中就不在记账；  0 = 不用区分
	  * @param VAE01  用于入科时记账
	  * @param lmode  --记账类别  0=默认对全部项目记账 1=只对床位费(A)记账 2=只对护理费(B)记账 3=只对设置的自动记账项目(C)记账
      --4=只对护理+设置的自动记账项目(B+C)记账 5=只对床位+设置的自动记账项目(A+C)记账 6=只对床位+护理(A+B)记账
	  * @return
	  * @throws Exception
	  */
	 public ModelVo execClinicVAJ1AutoInsert(
			 String lDate,int lFlag,long VAE01,int lmode
			 )throws Exception{
		 ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		
		  String date = DateUtils.getDateString(new Date());
		  if("0".equals(lDate)){
			  lDate=date;
		  }
		  String adate = date;
		  String BDate = adate.substring(0, 11);
		  String EDate = BDate+" 23:59:59";
		  String lStr="00000000";
		  int editVdc = 0;
		  Map map = new HashMap();
		  map.put("EDate", EDate);
		  map.put("lDate", lDate);
		  map.put("lFlag", lFlag);
		  map.put("VAE01", VAE01);
		  map.put("lmode", lmode);
		  map.put("lStr", lStr);
		  if(!params1(map)){
			  mo.setResult(StaticKeys.OH_FALSE);
			  return mo;
		  }
		  if(!params2(map)&&!params3(map)&&!params4(map)){
			  mo.setResult(StaticKeys.OH_FALSE);
			  return mo;
		  }
		  //全部自动记账项目
		  List TmpA = getAutoItem(map);
		  map.put("TmpA", TmpA);
		  List TmpPat = tempPAT(map);
		  map.put("TmpPat", TmpPat);
		  List TmpB = tempVAA1(map);
		  map.put("TmpB", TmpB);
		  
		  if((VAE01 == 0)&&(lmode==0||lmode==3||lmode==4||lmode==5)&&params5(map)){
			  editVdc =1;
			  if(editVdc == 1){
				  if(expenseDao.getkbmvaj(map)>0){
					  TmpB = getTempB(map);
					  TmpPat =getTempPat(map);
					  map.put("TmpB", TmpB);
					  map.put("TmpPat", TmpPat);
				  }else{
					  editVdc =0; 
				  }
				  
			  }
		  }
		  //修改收费单据 收费明细
		  updateVAI2_VAJ2(map);
		  //获取变更应收金额
		  TmpPat = getTmp_Pat(map);
		  map.put("TmpPat", TmpPat);
		  //新增余额记录
		  if(checkVBM1(map)){
			  insertVBM1(map);
		  }
          if(checkVBM2(map)){
        	  insertVBM2(map);
		  }
          //执行医嘱项目表
          if(editVdc ==1){
        	  updateVDC1(map);
        	  
          }
		 return mo;
	 }
	 private void updateVDC1(Map map)throws Exception{
		 expenseDao.updateVDC1(map);
	 }
	 private void insertVBM1(Map map)throws Exception{
		 map.put("lVBM01", getNewId.nextId());
		 expenseDao.insertVBM1(map);
	 }
     private void insertVBM2(Map map)throws Exception{
    	 map.put("lVBM01", getNewId.nextId());
		 expenseDao.insertVBM2(map);
	 }
	 private boolean checkVBM1(Map map)throws Exception{
		 return expenseDao.checkVBM1(map)>0;
	 }
     private boolean checkVBM2(Map map)throws Exception{
		 return expenseDao.checkVBM2(map)>0;
	 }
	 
	 private List getTmp_Pat(Map map)throws Exception{
		 return expenseDao.getTmp_Pat(map);
	 }
	 private void updateVAI2_VAJ2(Map map)throws Exception{
		 expenseDao.updateVAI2_VAJ2(map);
	 }
	 private List getTempB(Map map)throws Exception{
		 map.put("lid1", getNewId.nextId());
		 map.put("lid", getNewId.nextId());
		 return expenseDao.getTempB(map);
	 }
	 private List getTempPat(Map map)throws Exception{
		 map.put("lid", getNewId.nextId());
		 return expenseDao.getTempPat(map);
	 }
	 private boolean params1(Map map)throws Exception{
		 return expenseDao.params1(map)>0;
	 }
	 private boolean params2(Map map)throws Exception{
		 return expenseDao.params2(map)==0;
	 }
	 private boolean params3(Map map)throws Exception{
		 return expenseDao.params3(map)==0;
	 }
	 private boolean params4(Map map)throws Exception{
		 return expenseDao.params4(map)==0;
	 }
	 private boolean params5(Map map)throws Exception{
		 return expenseDao.params5(map)>0;
	 }
	 private List getAutoItem(Map map)throws Exception{
		  return expenseDao.getAutoItem(map);
	 }
	 private List tempVAA1(Map map)throws Exception{
		 List<Map<String,Object>> list = expenseDao.tempVAA1(map);
		 for (Map<String, Object> map2 : list) {
	    		long id=getNewId.nextId();
				map2.put("vaj01",id);
			}
		 return list;
	 }
     private List tempPAT(Map map)throws Exception{
    	List<Map<String,Object>> list = expenseDao.tempPAT(map);
    	for (Map<String, Object> map2 : list) {
    		long id=getNewId.nextId();
			map2.put("vai01",id);
			map2.put("vai04", "ZD"+id);
		}
    	
    	return list;
	 }
  
}
